package vn.student.controller;

import java.util.ArrayList;
import java.util.Date;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewInputTextBox;
import vn.student.mylib.Class;
import vn.student.mylib.ClassType;
import vn.student.mylib.Constants;
import vn.student.mylib.Contact;
import vn.student.mylib.ContactList;
import vn.student.mylib.WeekDay;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewEditClassDetailController {
	Activity act=null;
	String id = null;
	public Class classInfo = null;
	AStudentDB myDB = null;
	boolean isCreateNew = false;
	public ViewEditClassDetailController(Activity act, String id, boolean isCreateNew) {
		// TODO Auto-generated constructor stub
		this.act = act;
		this.id = id;
		myDB = new AStudentDB(act);
		this.isCreateNew = isCreateNew;
		classInfo = myDB.getClassInfo(id);
	}
	public ViewEditClassDetailController(Activity act, String id, boolean isCreateNew,String courseID) {
		// TODO Auto-generated constructor stub
		this.act = act;
		this.id = id;
		myDB = new AStudentDB(act);
		this.isCreateNew = isCreateNew;
		Date tmp = new Date();
		String dateStr = "";
		String str = "";
		str = String.valueOf(tmp.getYear()+1900);
		dateStr += str + "-";
		str = String.valueOf(tmp.getMonth()+1);
		str = str.length()==2?str:("0"+str);
		dateStr += str + "-";
		str = String.valueOf(tmp.getDate());
		str = str.length()==2?str:("0"+str);
		dateStr += str;
		
		Time currentTime = new Time();
		currentTime.setToNow();
		
		String time = "";
		
		str = String.valueOf(currentTime.hour);
		str = str.length()==2?str:("0"+str);
		time += str+":";
		str = String.valueOf(currentTime.minute);
		str = str.length()==2?str:("0"+str);
		time += str+":00";
		
		classInfo = new Class("New Class","LEC",courseID,"",dateStr,dateStr,time,time,"0","");
	}
	public void showClassDetailEditor(String id) {
		if(classInfo!=null){
			TextView tmp = (TextView) act.findViewById(R.id.classNameLabel);
			tmp.setText(classInfo.className); 
			tmp = (TextView) act.findViewById(R.id.durationLabel);
			tmp.setText(classInfo.dateStart + " to " + classInfo.dateEnd);
			tmp = (TextView) act.findViewById(R.id.locationLabel);
			tmp.setText(classInfo.location);
			tmp = (TextView) act.findViewById(R.id.timeLabel);
			tmp.setText(classInfo.timeStart+" to "+classInfo.timeEnd);
			tmp = (TextView) act.findViewById(R.id.typeLabel);
			tmp.setText(ClassType.getName(classInfo.type));
			tmp = (TextView) act.findViewById(R.id.instructorNameLabel);
			tmp.setText(classInfo.instructorName);
			if(classInfo.repeat.equals("0")){
				setNoneRepeatView();
			}
			else
				if(classInfo.repeat.equals("1")){
					setRepeatWeeksView();
				}
				else{
					setRepeatWorkingDaysView();
				}
		}
		
	}
	private void setRepeatWeeksView(){ 
		RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.day);
		rl.removeAllViews();
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.week_day, rl); 
		TextView tmp = (TextView) act.findViewById(R.id.repeatLabel);
		addWeekDayListener(); 
		int rlId = WeekDay.getDayId(classInfo.dateRepeat);
		rl = (RelativeLayout) act.findViewById(rlId);
		rl.setBackgroundResource(R.drawable.week_day_2);
		tmp.setText("Weeks");
	}
	private void setRepeatWorkingDaysView(){
		RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.day);
		rl.removeAllViews();
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.week_day, rl); 
		TextView tmp = (TextView) act.findViewById(R.id.repeatLabel);
		for(int i=1;i<WeekDay.num.length-1;i++){
			rl = (RelativeLayout) act.findViewById(WeekDay.num[i]);
			rl.setBackgroundResource(R.drawable.week_day_2);
		}
		tmp.setText("Working days");
	}
	private void setNoneRepeatView(){
		TextView tmp = (TextView) act.findViewById(R.id.repeatLabel);
		RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.day);
		rl.removeAllViews();
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.none_repeat_date, rl); 
		TextView tmp1 = (TextView) act.findViewById(R.id.repeatDateLabel);
		tmp1.setText(classInfo.dateStart);
		tmp.setText("None repeat");
	}
	public void showClassDetailEditor() {
		if(classInfo!=null){
			TextView tmp = (TextView) act.findViewById(R.id.classNameLabel);
			tmp.setText(classInfo.className); 
			tmp = (TextView) act.findViewById(R.id.durationLabel);
			tmp.setText(classInfo.dateStart + " to " + classInfo.dateEnd);
			tmp = (TextView) act.findViewById(R.id.locationLabel);
			tmp.setText(classInfo.location);
			tmp = (TextView) act.findViewById(R.id.timeLabel);
			tmp.setText(classInfo.timeStart+" to "+classInfo.timeEnd);
			tmp = (TextView) act.findViewById(R.id.typeLabel);
			tmp.setText(ClassType.getName(classInfo.type));
			if(classInfo.repeat.equals("0")){
				setNoneRepeatView();
			}
			else
				if(classInfo.repeat.equals("1")){
					setRepeatWeeksView();
				}
				else{
					setRepeatWorkingDaysView();
				}
		}
		//addWeekDayListener();
	}
	private void addWeekDayListener(){
		RelativeLayout btn = null;
		int i = 0; 
		for(i=0;i<7;i++){
			btn = (RelativeLayout) act.findViewById(WeekDay.getDayId(i));
			//btn = (RelativeLayout) act.findViewById(WeekDay.getDayId(2));
			btn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					editDateRepeat(WeekDay.getDayNameById(v.getId()));
				}
			});
		}
	}
	private void viewEditor(String oldValue,String title){
		Intent intent = new Intent(act,ViewInputTextBox.class);
		Bundle bundle = new Bundle();
		bundle.putString("title", title);
		bundle.putString("oldValue",oldValue);
		intent.putExtras(bundle);
		act.startActivityForResult(intent, Constants.ViewEditClassDetailEditorRequestCode);
	}
	public void editClassName(String name){
		if(classInfo==null)
			return;
		classInfo.className = name;
		TextView tmp = (TextView) act.findViewById(R.id.classNameLabel);
		tmp.setText(classInfo.className);
	}
	public void editLocation(String location) {
		if(classInfo==null)
			return;
		classInfo.location = location;
		TextView tmp = (TextView) act.findViewById(R.id.locationLabel);
		tmp.setText(classInfo.location);
	}
	public void editTime(String timeStartStr, String timeEndStr) {
		if(classInfo==null)
			return;
		classInfo.timeStart = timeStartStr;
		classInfo.timeEnd = timeEndStr;
		TextView tmp = (TextView) act.findViewById(R.id.timeLabel);
		tmp.setText(classInfo.timeStart + " to " + classInfo.timeEnd);
		
	}
	public void editInstructor(String id,String name) {
		classInfo.instructorID = id;
		classInfo.instructorName = name;
		TextView tmp = (TextView) act.findViewById(R.id.instructorNameLabel);
		tmp.setText(classInfo.instructorName);
	}
	public void editType(String classTypeCode) {
		classInfo.type = classTypeCode;
		TextView tmp = (TextView) act.findViewById(R.id.typeLabel);
		tmp.setText(ClassType.getName(classInfo.type));
	}
	public void editRepeat() {
		
	}
	public boolean saveClassInfo() {
		// TODO Auto-generated method stub
		if(isCreateNew){
			 return getVNStudentDB().addNewClass(classInfo);
		}
		else
			return getVNStudentDB().updateClassDetail(classInfo);
	}
	private AStudentDB getVNStudentDB(){
		if(myDB==null){
			myDB = new AStudentDB(act);
		}
		return myDB;
	}
	public void editDuration(String dateStartStr, String dateEndStr) {
		classInfo.dateStart=dateStartStr;
		classInfo.dateEnd=dateEndStr;
		TextView tmp = (TextView)act.findViewById(R.id.durationLabel);
		tmp.setText(classInfo.dateStart+" to "+classInfo.dateEnd);
		
	}
	public ArrayList<Contact> getInstructor() {
		ContactList tmp =  myDB.getContactList("gv");
		return tmp.getContactInfoList();
	}
	public void editDateRepeat(String str) {
		RelativeLayout rl = (RelativeLayout) act.findViewById(WeekDay.getDayId(classInfo.dateRepeat));
		rl.setBackgroundResource(R.drawable.week_day);
		classInfo.dateRepeat = str;
		int rlId = WeekDay.getDayId(str);
		rl = (RelativeLayout) act.findViewById(rlId);
		rl.setBackgroundResource(R.drawable.week_day_2);
	}
	public void viewNoneRepeatView() {
		classInfo.repeat = "0";
		classInfo.dateRepeat = "MON";
		setNoneRepeatView();
	}
	public void viewRepeatWeeksView() {
		classInfo.repeat = "1";
		classInfo.dateRepeat = "MON";
		setRepeatWeeksView();
	}
	public void viewRepeatWorkingDays() {
		classInfo.repeat = "2";
		classInfo.dateRepeat = "MON";
		setRepeatWorkingDaysView();
	}
}
